package models

import "guard/internal/tool"

/**
中间表用join查
*/

// User 简单的用户信息，这个用户信息可以自定义
type User struct {
	Id int `json:"id,omitempty" gorm:"column:id;primary_key"`
	// 用户名，可以是电话号码
	Username string `json:"username,omitempty" gorm:"column:username;type:varchar(16)"  binding:"required"`
	Password string `json:"password,omitempty" gorm:"column:password;type:varchar(100)"  binding:"required"`
	// 社交信息
	Nickname string `json:"nickname,omitempty" gorm:"column:nickname;type:varchar(20)"  binding:"required"`
	Picture  string `json:"picture,omitempty" gorm:"column:picture;type:varchar(300)"  binding:"required"`
	// 是否禁用
	Enable bool `json:"enable,omitempty" gorm:"column:enable"`

	// 范围标签
	UserScopeTag string `json:"user_scope_tag" gorm:"column:user_scope_tag"`
}

func (User) TableName() string {
	return "user"
}

// UserInfo 返回的用户信息
type UserInfo struct {
	Id       int    `json:"id,omitempty"`
	Username string `json:"username,omitempty"`
	Nickname string `json:"nickname,omitempty"`
	Picture  string `json:"picture,omitempty"`
}

// UserDto user映射，用于接收和返回
type UserDto struct {
	Id           int      `json:"id,omitempty"`
	Username     string   `json:"username,omitempty" binding:"required"`
	Password     string   `json:"password,omitempty"`
	Nickname     string   `json:"nickname,omitempty" binding:"required"`
	Picture      string   `json:"picture,omitempty"`
	Enable       bool     `json:"enable" binding:"required"`
	UserScopeTag []string `json:"user_scope_tag" binding:"required"`
}

func NewUserDto(user *User) *UserDto {
	return &UserDto{
		Id:           user.Id,
		Username:     user.Username,
		Password:     "***",
		Nickname:     user.Nickname,
		Picture:      user.Picture,
		Enable:       user.Enable,
		UserScopeTag: tool.StringListDestroy(user.UserScopeTag),
	}
}

func (uto *UserDto) Serialization() *User {
	return &User{
		Id:           uto.Id,
		Username:     uto.Username,
		Password:     uto.Password,
		Nickname:     uto.Nickname,
		Picture:      uto.Picture,
		Enable:       uto.Enable,
		UserScopeTag: tool.StringListBuild(uto.UserScopeTag),
	}
}

/**
权限管理完全交给了scope
*/
